package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.core.constant.CommonConstants;
import com.geovis.emergency.spd.biz.pc.mapper.PcPlanDutyOrgDeptMapper;
import com.geovis.emergency.spd.biz.pc.mapper.PcPlanDutyOrgMapper;
import com.geovis.emergency.spd.biz.pc.mapper.PcPlanOrgUserMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcPlanDutyOrgService;
import com.geovis.emergency.spd.biz.pc.service.IPcPlanOrgUserService;
import com.geovis.emergency.spd.entity.pc.entity.PcPlanDutyOrg;
import com.geovis.emergency.spd.entity.pc.entity.PcPlanDutyOrgDept;
import com.geovis.emergency.spd.entity.pc.entity.PcPlanOrgUser;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcPlanDutyOrgDeptVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcPlanDutyOrgVO;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 应急预案指挥模块 - 预案组织架构 服务实现类
 * </p>
 *
 * @author Lonsho
 * @since 2022-07-11
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class PcPlanDutyOrgServiceImpl extends ServiceImpl<PcPlanDutyOrgMapper, PcPlanDutyOrg> implements IPcPlanDutyOrgService {
    //组织架构与成员单位的关联
    private final PcPlanDutyOrgDeptMapper pcPlanDutyOrgDeptMapper;

    private final IPcPlanOrgUserService pcPlanOrgUserService;

    private List<String> getAllDutyOrgId(List<PcPlanDutyOrgVO> list){
        List<String> idList = Lists.newArrayList();
        for (PcPlanDutyOrgVO pcPlanDutyOrgVO : list) {
            idList.add(pcPlanDutyOrgVO.getId());
            if (CollUtil.isNotEmpty(pcPlanDutyOrgVO.getChildren())) {
                List<String> allDutyOrgId = getAllDutyOrgId(pcPlanDutyOrgVO.getChildren());
                idList.addAll(allDutyOrgId);
            }
        }
        return idList;
    }
    @Override
    public void removeDutyOrg(List<PcPlanDutyOrgVO> list,String pcPlanId){
        List<String> allDutyOrgId = getAllDutyOrgId(list);
        baseMapper.delete(
            Wrappers.lambdaQuery(PcPlanDutyOrg.class)
               .eq(PcPlanDutyOrg::getPcPlanId,pcPlanId)
               .notIn(PcPlanDutyOrg::getId,allDutyOrgId)
        );
        pcPlanDutyOrgDeptMapper.delete(
                Wrappers.lambdaQuery(PcPlanDutyOrgDept.class)
                        .eq(PcPlanDutyOrgDept::getPcPlanId,pcPlanId)
                        .notIn(PcPlanDutyOrgDept::getPcPlanOrgId,allDutyOrgId)
        );
    }
    @Override
    public void savePlanDutyOrg(List<PcPlanDutyOrgVO> list, String pcPlanId, String parentId, String ancestors) {
        if (ObjectUtil.isNotEmpty(list)){
//            AtomicInteger idx = new AtomicInteger(0);

            for (PcPlanDutyOrgVO planDutyOrgVO : list) {
                PcPlanDutyOrg newPcPlanDutyOrg = BeanUtil.toBean(planDutyOrgVO, PcPlanDutyOrg.class);
                PcPlanDutyOrg pcPlanDutyOrg = baseMapper.selectById(planDutyOrgVO.getId());
                if (ObjectUtil.isNotNull(pcPlanDutyOrg)){
                    String id = pcPlanDutyOrg.getId();
                    newPcPlanDutyOrg.setId(id);
                    newPcPlanDutyOrg.setCreateTime(pcPlanDutyOrg.getCreateTime());
                    newPcPlanDutyOrg.setCreateDept(pcPlanDutyOrg.getCreateDept());
                    newPcPlanDutyOrg.setCreateBy(pcPlanDutyOrg.getCreateBy());
                    newPcPlanDutyOrg.setFullName(pcPlanDutyOrg.getFullName());
                }else {
                    newPcPlanDutyOrg.setId(null);
                    newPcPlanDutyOrg.setCreateTime(null);
                    newPcPlanDutyOrg.setCreateDept(null);
                    newPcPlanDutyOrg.setCreateBy(null);
                }
                newPcPlanDutyOrg.setUpdateTime(null);
                newPcPlanDutyOrg.setUpdateBy(null);
//                newPcPlanDutyOrg.setIdx(Long.valueOf(idx.incrementAndGet()+""));
                newPcPlanDutyOrg.setPcPlanId(pcPlanId);
                if (StrUtil.isEmpty(parentId)){
                    newPcPlanDutyOrg.setParentId(CommonConstants.TREE_ROOT_CODE);
                    newPcPlanDutyOrg.setAncestors(CommonConstants.TREE_ROOT_CODE);
                }else {
                    newPcPlanDutyOrg.setParentId(parentId);
                    newPcPlanDutyOrg.setAncestors(ancestors);
                }
                if (StrUtil.isNotBlank(newPcPlanDutyOrg.getId())){
                    baseMapper.updateById(newPcPlanDutyOrg);
                }else {
                    baseMapper.insert(newPcPlanDutyOrg);
                }

                //保存组织架构与人员的关联
                if (ObjectUtil.isNotEmpty(planDutyOrgVO.getDutyOrgDeptList())){
                    //设置机构id，预案id
                    planDutyOrgVO.getDutyOrgDeptList().stream().forEach(vo->vo.setPcPlanOrgId(newPcPlanDutyOrg.getId()));
                    planDutyOrgVO.getDutyOrgDeptList().stream().forEach(vo->vo.setPcPlanId(pcPlanId));
                    pcPlanOrgUserService.saveBatch(planDutyOrgVO.getDutyOrgDeptList());
                }
                savePlanDutyOrg(planDutyOrgVO.getChildren(),pcPlanId,newPcPlanDutyOrg.getId(),newPcPlanDutyOrg.getAncestors()+","+newPcPlanDutyOrg.getId());
            }
        }
    }
}
